package com.smu.recognize.quartz;
import com.smu.recognize.Util.CurrentTimeUtil;
import com.smu.recognize.model.Apply;
import com.smu.recognize.model.Attendance;
import com.smu.recognize.model.Statics;
import com.smu.recognize.model.User;
import com.smu.recognize.mqtt.MqttPushClient;
import com.smu.recognize.service.ApplyService;
import com.smu.recognize.service.AttendanceService;
import com.smu.recognize.service.StaticsService;
import com.smu.recognize.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ScheduledTasks {
    //每周日零点统计考勤信息
    @Autowired
    UserService userService;
    @Autowired
    AttendanceService attendanceService;
    @Autowired
    ApplyService applyService;
    @Autowired
    StaticsService staticsService;

    List<Integer> user_idList=new ArrayList<>();
    List<Attendance> attList=new ArrayList<>();
    List<Apply> appList=new ArrayList<>();

    //每月最后一天23点执行一次：0 0 23 L * ?
    //每周星期天凌晨1点实行一次：0 0 1 ? * L
    //每天凌晨1点执行一次：0 0 1 * * ?
    //每两分钟 0 0/2 * * * ?
    //每天凌晨1点执行一次
    @Scheduled(cron = "0 0 1 * * ?")
    public void work() throws Exception {
        //List<User> userList=mapper.selectAll();
        //new Date()+userList.toString()
        log.info("执行调度任务："+new Date());
        //定时更新考勤统计数据
        //当前年月份
        String date= CurrentTimeUtil.getTime();
        date=date.substring(0,7);
        // System.out.println(date);//如2021-03
        user_idList=userService.findUserID();
        for (int x:user_idList){
            Statics statics=new Statics();
            int i = 0;//总考勤
            int j=0;//月考勤
            int p=0;//迟到次数
            int q=0;//早退次数
            int r=0;//出差次数
            int t=0;//出差时间
            int z=0;//请假次数
            int u=0;//请假时长
            int w=0;//外出次数
            int s=0;//加班次数
            int hours=0;//加班时长
            String name;//用户姓名

            appList=applyService.findByUser_id(x);
            attList=attendanceService.findAttendanceByUser_id(x);
            name=userService.findNameByUser_id(x);
            //出差、请假、外此、加班统计
            for (Apply apply : appList) {
                if (apply.getA_status() == 0 || apply.getA_status() == 2) {
                    continue;
                } else {
                    if (apply.getA_type().equals("出差")) {
                        r++;
                        t+=apply.getA_hours();
                    } else if (apply.getA_type().equals("请假")) {
                        z++;
                        u+=apply.getA_hours();
                    } else if (apply.getA_type().equals("外出")) {
                        w++;
                    } else if (apply.getA_type().equals("加班")) {
                        s++;
                        hours += apply.getA_hours();
                    }
                }
            }

            for (Attendance attendance9 : attList) {
                if (attendance9.getStatus() == 1) {
                    //总正常考勤次数
                    i++;
                }
                if (attendance9.getDate().startsWith(date)) {
                    //这个月考勤次数
                    j++;
                    if (attendance9.getDescribed().equals("迟到")) {
                        //这个月迟到次数
                        p++;
                    } else if (attendance9.getDescribed().equals("早退")) {
                        //这个月早退次数
                        q++;
                    }
                }
            }
            statics.setUser_id(x);
            statics.setName(name);
            statics.setDate(date);
            statics.setWork_day(j);
            statics.setLater_day(p);
            statics.setEarly_day(q);
            statics.setTravel_day(r);
            statics.setTravel_time(t);
            statics.setLeave_day(z);
            statics.setLeave_time(u);
            //缺勤
            //statics.setAbsent_day();
            statics.setOver_day(s);
            statics.setOver_time(hours);
            //没有本月的统计
            if (staticsService.selectOne(statics.getDate(),statics.getUser_id())==null){
                log.info(statics.toString());
                //插入
                boolean flag=staticsService.insert(statics);

                log.info("定时任务插入"+flag);
            }else {
                //更新
                boolean flag= staticsService.update(statics);
                log.info("定时任务更新"+flag);
            }
        }
    }
    //周一至周五0 0 8 ? * 2,3,4,5,6 *八点
    //每两分钟 0 0/2 * * * ?
    @Scheduled(cron = "0 0 8 ? * 2,3,4,5,6")
    public void morningPublish(){
        MqttPushClient client=MqttPushClient.getInstance();
        client.morningPublish();
    }
    @Scheduled(cron = "0 0 19 ? * 2,3,4,5,6")
    public void afternooonPublish(){
        MqttPushClient client=MqttPushClient.getInstance();
        client.afternooonPublish();
    }
/*    @Scheduled(cron = "0 0/1 * * * ?")
    public void testPublish(){
        MqttPushClient client=MqttPushClient.getInstance();
        client.testPublish();
    }*/
}






